package com.aiyige.page.photo;


import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseFragment;
import com.aiyige.base.eventbus.EventPhotoTap;
import com.aiyige.page.photo.model.Photo;
import com.aiyige.page.player.PlayerInfo;
import com.aiyige.utils.WebImageUrlModifier;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class PhotoDataPage extends BaseFragment {
    Photo photo;
    @BindView(R.id.photoView)
    SubsamplingScaleImageView photoView;
    @BindView(R.id.playBtn)
    ImageView playBtn;
    @BindView(R.id.simpleIv)
    ImageView simpleIv;
    Unbinder unbinder;

    public PhotoDataPage() {

    }

    public Photo getPhoto() {
        return photo;
    }

    public void setPhoto(Photo photo) {
        this.photo = photo;
    }

    public static PhotoDataPage newInstance(Photo photo) {
        PhotoDataPage fragment = new PhotoDataPage();
        fragment.setPhoto(photo);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page_photo_data, container, false);
        unbinder = ButterKnife.bind(this, view);
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new EventPhotoTap());
            }
        });
        simpleIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new EventPhotoTap());
            }
        });
        playBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<PlayerInfo> playList = new ArrayList<>();
                playList.add(new PlayerInfo(photo.getVideoUrl(), "", photo.getUrl(), true));
                ARouter.getInstance().build(ARouterConfig.PlayerPage).withSerializable("playList", (Serializable) playList).navigation();
            }
        });
        switch (photo.getMediaType()) {
            case Photo.MEDIA_TYPE_PHOTO:
                photoView.setVisibility(View.INVISIBLE);
                simpleIv.setVisibility(View.INVISIBLE);
                playBtn.setVisibility(View.INVISIBLE);
                Glide.with(getContext())
                        .load(photo.getUrl())
                        .apply(RequestOptions.fitCenterTransform())
                        .into(new SimpleTarget<Drawable>(1080, 1920) {
                            @Override
                            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                                try {
                                    photoView.setVisibility(View.INVISIBLE);
                                    simpleIv.setVisibility(View.INVISIBLE);
                                    if (resource instanceof GifDrawable) {
                                        simpleIv.setVisibility(View.VISIBLE);
                                        GifDrawable gifDrawable = (GifDrawable) resource;
                                        simpleIv.setImageDrawable(resource);
                                        gifDrawable.start();
                                    } else if (resource instanceof BitmapDrawable) {
                                        photoView.setVisibility(View.VISIBLE);
                                        photoView.setImage(ImageSource.bitmap(((BitmapDrawable) resource).getBitmap()));
                                    }
                                } catch (Exception e) {

                                }
                            }
                        });
                break;
            case Photo.MEDIA_TYPE_VIDEO:
                photoView.setVisibility(View.INVISIBLE);
                simpleIv.setVisibility(View.VISIBLE);
                playBtn.setVisibility(View.VISIBLE);
                Glide.with(getContext())
                        .asBitmap()
                        .load(photo.getUrl())
                        .apply(RequestOptions.placeholderOf(android.R.color.transparent))
                        .transition(BitmapTransitionOptions.withCrossFade())
                        .into(simpleIv);
                break;
        }


        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected void initData() {

    }
}
